package com.wmh.baseservice.operationaid.controller;

import com.alibaba.fastjson2.JSON;

import com.wmh.baseservice.operationaid.entity.Category;
import com.wmh.baseservice.operationaid.pojo.vo.category.CategoryReqVO;
import com.wmh.baseservice.operationaid.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.error;
import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

/**
 * @author dongaolong
 * @createDate 2023-06-05
 */
@Validated
@RestController
@RequestMapping("operationAid/Category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation("新增")
    @PostMapping("/add")
    public String add(CategoryReqVO categoryReqVO){
        categoryService.add(categoryReqVO);
        return success();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public String delete(String ids) {
        List<Long> idList = JSON.parseArray(ids, Long.class);
        if (!categoryService.removeByIds(idList)) {
            return error("删除失败");
        }
        return success();
    }

    @ApiOperation("列表查询")
    @PostMapping("/categoryList")
    public ResponseEntity<List<Category>> getList() {
        List<Category> categories = categoryService.getList();
        return ResponseEntity.ok(categories);
    }
}